//	--------------------------------------------------------------------
//	Copyright(C) 2006,2007 Zhao Yukun. All rights reserved.
//	This file is a part of the Xeres system.
//	Xeres 2007
//	Contact : xeres.engine@gmail.com
//	$(reservedInfo)
//	$(reservedDeclaration)
//	--------------------------------------------------------------------
///	@file	<timer_center.h>
///	@path	~/src/engine/xcore/
///	@date	2007/12/02
///	@desc	Timer center.

#pragma once

#include "xeres.h"

#include <map>

#include "lib/utilities/time.h"
#include "lib/utilities/callback.h"

#include "graphics/animation/frame.h"

namespace xeres {

	/*!
		\class	TimerCenter
		\brief	Encapsulates a timer control.
	*/
	class TimerCenter
	{
	protected:

		/// \name Constructor & Destructor
		//@{

		/// \ctor
		TimerCenter( void );

		/// \dtor
		~TimerCenter( void );

		//@}

	protected:

		/// \name Internal Manipulation
		//@{

		/*!
			\brief		Initialize timer center.
		*/
		void Initialize( void );

		/*!
			\brief		Finalize timer center.
		*/
		void Finalize( void );

		/*!
			\brief		Update timer & do callback.
		*/
		void Tick( const Frame& frame );

		//@}

	public:

		/// \name Interface Manipulation
		//@{

		/*!
			\brief		Insert new timer, the call will run after specific second(s).
		*/
		void InsertTimer( float run_after_sec , function<void(float)> call );

		//@}

	private:

		friend class Engine;

		// calls
		typedef std::pair< float , function<void(float)> > _CallBack;

		// timer collect
		typedef std::multimap< float , function<void(float)> > _TimerQueue;

	//@ data

		// stats
		bool					m_ticking;
		bool					m_spare0[3];

		// current stamp
		float					m_stamp;

		std::deque< function<void(float)> >	m_immediate;

		// in 3 second's callback will goes to here
		_TimerQueue 			m_timers;

		// nesting calls, which inserted while ticking
		_TimerQueue 			m_nestingTimers;
	};

} // namespace xeres
